import numpy as np
import time
def traangle_wave(x,c,c_0,hc):
    x=x-int(x)
    if x>c:
        r=0.0
    elif x<c_0:
        r=x/c_0*hc 
    else:
        r=(c-x)/(c-c_0)*hc 
    return r
triangle_ufunc1=np.frompyfunc(traangle_wave,4,1)
triangle_ufunc2=np.frompyfunc(lambda x:traangle_wave(x,0.6,0.4,1.0),1,1)
triangle_ufunc3=np.vectorize(traangle_wave,otypes=[np.float64])
x=np.linspace(0,2,1000)
t0=time.perf_counter()
y1=np.array([traangle_wave(t,0.6,0.4,1.0) for t in x])
t1=time.perf_counter()-t0
y2=triangle_ufunc1(x,0.6,0.4,1)
t2=time.perf_counter()-t1
y3=triangle_ufunc2(x)
t3=time.perf_counter()-t2
y4=triangle_ufunc3(x,0.6,0.4,1.0)
t4=time.perf_counter()
print(f"t1:{t1} t2: {t2} t3:{t3} t4: {t4}")